package mingshin;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class D710 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input;
		StringBuffer output;

		while((input = br.readLine()) != null) {
			String[] spInput = input.split(" ");
			int n = Integer.parseInt(spInput[0]);
			int m = Integer.parseInt(spInput[1]);

			ParkingLot parkingLot = new ParkingLot(n, br);
			
			output = new StringBuffer();
			output.append(parkingLot.query(m));
			
			System.out.println(output);
			
			if((input = br.readLine()) == null)
				System.exit(0);
		}
	}
}

class Car {
	private String brand;
	private String color;
	
	public Car() {
		
	}
	public Car(String brand, String color) {
		this.brand = brand;
		this.color = color;
	}

	public String getBrand() { return brand; }
	public String getColor() { return color; }
}

class ParkingLot {
	private int n;
	private int m;
	private BufferedReader br;
	private Car[] cars;
	
	public ParkingLot() {
		
	}
	public ParkingLot(int n, BufferedReader br) throws IOException {
		this.n = n;
		this.br = br;
		parking();
	}
	
	public void parking() throws IOException {
		cars = new Car[n];
		
		String input;
		String[] spInput;		
		for(int i = 0; i < cars.length; i++) {
			input = br.readLine();
			spInput = input.split(" ");
			
			cars[i] = new Car(spInput[0], spInput[1]);
		}
	}
	
	public String query(int m) throws IOException {
		String im = "";
		String[][] condition = new String[m][2];
		
		String input;
		String[] spInput;
		for(int i = 0; i < condition.length; i++) {
			input = br.readLine();
			spInput = input.split(" ");
			
			condition[i][0] = spInput[0];
			condition[i][1] = spInput[1];
		}

		for(int i = 0; i < condition.length; i++) {
			for(int j = 0; j < cars.length; j++) {
				if(condition[i][0].equals("brand")) {
					if(cars[j].getBrand().equals(condition[i][1]))
						im += cars[j].getBrand() + " " + cars[j].getColor() + "\n";
				} else {
					if(cars[j].getColor().equals(condition[i][1]))
						im += cars[j].getBrand() + " " + cars[j].getColor() + "\n";
				}
			}
		}
		
		return im;
	}
}